package dao;

import java.util.List;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import model.Usuario;

public class UsuarioDAO extends GenericDAO {
    
    Usuario aluno = new Usuario();
    
    public Usuario findById(Long id){
        try{
            this.OpenConection();
            aluno = this.em.find(Usuario.class, id);
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Erro ao consultar - "+e.getMessage());
        }finally{
            this.CloseConection();
        }
        return aluno;
    }
    
    public Usuario findByUser(String user){
        try{
            this.OpenConection();
            Query consulta;
            consulta = em.createQuery("select u from Usuario u where u.user = :user");
            consulta.setParameter("user", user);
            aluno = (Usuario)consulta.getSingleResult();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Erro ao consultar - "+e.getMessage());
        }finally{
            this.CloseConection();
        }
        return aluno;
    }
    
    public boolean validUser(String user){
        boolean v = false;
        try{
            this.OpenConection();
            Query consulta;
            consulta = em.createQuery("select u from Usuario u where u.user = :user");
            consulta.setParameter("user", user);
            aluno = (Usuario)consulta.getSingleResult();
            if(aluno.getNome().equals(user)){
                v = true;
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Erro ao consultar - "+e.getMessage());
        }finally{
            this.CloseConection();
        }
        return v;
    }
    
    public void delete(Usuario a) {
        try {
            this.OpenConection();
            em.remove(em.getReference(Usuario.class, a.getId()));
            em.getTransaction().commit();
            JOptionPane.showMessageDialog(null, "Deletado com sucesso !");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro ao deletar - "+e.toString());
            em.getTransaction().rollback();
            System.out.println(e.getStackTrace());
            
        } finally {
            this.CloseConection();
        }
    }
    
    public List<Usuario> findAll(){
        try{
            this.OpenConection();
            Query consulta;
            consulta = em.createQuery("SELECT u FROM Usuario u", Usuario.class);
            List<Usuario> lista;
            lista = consulta.getResultList();
            return lista;
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Erro ao listar - "+e.toString());
            em.getTransaction().rollback();
            System.out.println(e.getStackTrace());
            return null;
        }
    }
    
    public String getLastID(){
        try{
            this.OpenConection();
            Query consulta;
            consulta = em.createQuery("SELECT u FROM Usuario u order by u.id desc", Usuario.class);
            List<Usuario> lista = consulta.getResultList();
            consulta.setMaxResults(1);
            aluno = (Usuario) consulta.getSingleResult();
            return ""+(aluno.getId()+1);
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Erro ao capturar ultimo ID - "+e.toString());
            em.getTransaction().rollback();
            System.out.println(e.getStackTrace());
            return null;
        }
    }
    
}//CLASS
